package com.drama.api.sys.info.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.ApiIndex;
import com.drama.api.base.Constants;
import com.drama.api.base.R;
import com.drama.api.base.RestPage;
import com.drama.api.company.order.domain.vo.CompanyOrderCountVO;
import com.drama.api.sys.company.service.ISysCompanyService;
import com.drama.api.sys.dramas.domain.vo.SysDramasCountSearchVO;
import com.drama.api.sys.dramas.domain.vo.SysDramasCountVO;
import com.drama.api.sys.dramas.service.ISysDramasService;
import com.drama.api.sys.info.domain.vo.*;
import com.drama.api.sys.order.domain.vo.SysOrderRechargeCountVO;
import com.drama.api.sys.order.service.IOrderRechargeService;
import com.drama.api.sys.user.domain.vo.SysUserPasswordUpdateVO;
import com.drama.api.sys.user.domain.vo.UserLoginReqVO;
import com.drama.api.sys.user.domain.vo.UserLoginResVO;
import com.drama.api.sys.user.service.ISysUserService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api( tags = ApiIndex.SysInfoController)
@RequestMapping(value = "/api/sys/info")
@RestController
public class SysInfoController {

    @Autowired
    private ISysUserService service;
    @Autowired
    private IOrderRechargeService rechargeService;
    @Autowired
    private ISysDramasService dramasService;

    @Autowired
    private ISysCompanyService companyService;


    @ApiOperation(value = "平台用户登录", notes = "登录后取到接口得到token(test/E10ADC3949BA59ABBE56E057F20F883E)")
    @PostMapping(value = "/login")
    @ResponseBody
    public R<UserLoginResVO> login(@ApiParam(value = "查询参数") @Valid @RequestBody UserLoginReqVO userLogin, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            UserLoginResVO userLoginResVO = service.login(userLogin);
            result.setData(userLoginResVO);
        }
        return result;
    }




    @ApiOperation(value = "修改当前登录用户密码")
    @PostMapping(value = "/update_password")
    @SaCheckLogin
    public R update_password(@ApiParam(value = "参数") @Valid @RequestBody SysUserPasswordUpdateVO passwordUpdateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
           service.updatePassword(passwordUpdateVO);
        }
        return result;
    }


    @ApiOperation(value = "查询充值收益统计")
    @GetMapping(value = "/recharge_count")
    @SaCheckLogin
    public R<SysOrderRechargeCountVO> recharge_count() {
        CompanyOrderCountVO countVO = rechargeService.getCount();
        return new R(countVO);
    }


    @ApiOperation(value = "查询云资源/流量情况统计")
    @GetMapping(value = "/num_count")
    @SaCheckLogin
    public R<CompanyStorageCountVO> num_count() {
        CompanyStorageCountVO countVO = companyService.getNumCount();
        return new R(countVO);
    }

    @ApiOperation(value = "获取当前登录人信息（包括入驻商户数量）")
    @GetMapping(value = "/one")
    @SaCheckLogin
    public R<InfoSysUserVO> one() {
        InfoSysUserVO infoSysUserVO = service.getOne();
        infoSysUserVO.setCompanyNum(companyService.getCount());
        return new R(infoSysUserVO);
    }



    @ApiOperation(value = "查询资源占用集合")
    @GetMapping(value = "/storage_list")
    public R<RestPage<List<CompanyStorageVO>>> storage_list(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyStorageSearchVO searchVO) {
        List<CompanyStorageVO> list = companyService.getStorageList(searchVO);
        RestPage restPage = new RestPage(new PageInfo(list), list);
        return new R(restPage);
    }


    @ApiOperation(value = "查询流量曲线(单位GB)")
    @GetMapping(value = "/flow_list")
    public R<List<CompanyFlowVO>> flow_list(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyFlowSearchVO searchVO) {
        List<CompanyFlowVO> list = companyService.getFlowList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "查询剧目审核状态统计")
    @GetMapping(value = "/drama_count")
    @SaCheckLogin
    public R<List<SysDramasCountVO>> count(@ApiParam(value = "查询参数") @ModelAttribute SysDramasCountSearchVO searchVO) {
        List<SysDramasCountVO> list = dramasService.count(searchVO);
        return new R(list);
    }



}